package com.hm.service.Impl;

import com.hm.dao.MenuMapper;
import com.hm.dao.StudentMapper;
import com.hm.pojo.Curriculum;
import com.hm.pojo.Menu;
import com.hm.pojo.Student;
import com.hm.service.StudentService;
import com.hm.utils.MybatisUtil;
import com.hm.utils.UuidUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * This class is for com.hm.service.Impl
 *
 * @author 李聪
 * @version 2023/10/22
 */
public class StudentServiceImpl implements StudentService {
   private StudentMapper studentMapper ;
   private MenuMapper menuMapper;
   private SqlSession sqlSession;

    @Override
    public Student login(String id, String password) {
        sqlSession=MybatisUtil.openSession();
        studentMapper = sqlSession.getMapper(StudentMapper.class);
        MenuMapper mapper = sqlSession.getMapper(MenuMapper.class);

        Student login = null;
        try {
             login = studentMapper.login(id, password);
            List<Menu> userList = mapper.getAllList();
            for (Menu menu : userList) {
                menu.setChildren(mapper.getbyid(menu.getId()));
            }
            for (Menu menu : userList) {
               if(menu.getChildren().isEmpty()){
                   menu.setChildren(null);
               }
            }
            login.setToken(UuidUtil.getUuid());
            login.setMenu(userList);


             //开启事务
            //sqlSession.commit();
        } catch (Exception e) {
            // TODO: handle exception
            sqlSession.rollback();
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return login;
    }

    @Override
    public List<Curriculum> show_curriculum(String grades) {
        studentMapper = sqlSession.getMapper(StudentMapper.class);
        List<Curriculum> curriculumList=null;
        try {
            curriculumList=studentMapper.show_curriculum(grades);

            //开启事务
            //sqlSession.commit();
        } catch (Exception e) {
            // TODO: handle exception
            sqlSession.rollback();
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return curriculumList;
    }

    /*@Override
    public List<Curriculum> find_Old_CurriculumById(String stu_id) {
        studentMapper=sqlSession.getMapper(StudentMapper.class);
        List<Curriculum> curriculumList=null;
        try {
            curriculumList=studentMapper.find_Old_CurriculumById(stu_id);

            //开启事务
            //sqlSession.commit();
        } catch (Exception e) {
            // TODO: handle exception
            sqlSession.rollback();
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return curriculumList;
    }*/
}
